import sys
import os.path 

cur_path = os.path.dirname(__file__)
parent_path = os.path.abspath(os.path.join(cur_path, '..'))
sys.path.append(parent_path)

import math
import time
import pymongo
from topapi import *
from exceptions import Exception


def get_traderates_info(inc_obj, user):
    r = []
    page_size = 200
    inc_info = inc_obj.get(user['top_session'], page_no=1, page_size=page_size)
    total_result = inc_info['total_results']
    if total_result <= 0:
        return r
    r.extend( inc_info['trade_rates']['trade_rate'] )
    page_num = int ( math.ceil(1.0 * total_result / page_size) )
    for page_no in range(2, page_num+1):
        inc_info = inc_obj.get(user['top_session'], page_no=page_no, page_size=page_size)
        r.extend( inc_info['trade_rates']['trade_rate'] )
    return r


def all_traderate_to_db(db, user):
    inc_obj = TradeRate()
    inc_tr = get_traderates_info(inc_obj, user)
    for itr in inc_tr:
        itr['_id'] = itr['oid']
        db.traderate.save(itr)


def traverse_and_process_inc():
    m = pymongo.Connection('127.0.0.1:57324')
    db = m.taogold 
    skip = 0
    limit = 100
    while True:
        cur = db.user.find(fields = ['top_session', ], timeout=False).skip(skip).limit(limit)
        if cur.count(True) <=0:
            break
        for u in cur:
            try:
                all_traderate_to_db(db, u)
            except Exception as e:
                print 'error in processing user increment traderate: %s, %s' %(u['_id'].encode('utf8'), repr(e))
                continue
            print 'processed: %s' %(u['_id'].encode('utf8'))
        skip += limit
    

if __name__ == '__main__':
    traverse_and_process_inc()

